package com.oauth.google.client.ui.docs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.google.client.beans.docs.GoogleDocModel;
import com.oauth.google.client.beans.docs.GoogleDocument;

public class HomePanel extends Composite {

	private static HomePanelUiBinder uiBinder = GWT
			.create(HomePanelUiBinder.class);

	interface HomePanelUiBinder extends UiBinder<Widget, HomePanel> {
	}

	@UiField
	VerticalPanel docsPanel;

	public HomePanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private VerticalPanel getDocument(final GoogleDocument pDocument) {
		// hPanel.add(new Image(pTweet.getUser().getProfileImageUrl()));
		VerticalPanel vPanel = new VerticalPanel();
		Anchor anchor = new Anchor(pDocument.getTitle(), true);
		anchor.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent pEvent) {
				Window.open(pDocument.getLink(), pDocument.getTitle(), null);
			}

		});
		vPanel.setSpacing(4);

		vPanel.add(anchor);
		return vPanel;
	}

	public void setData(GoogleDocModel pModel) {
		docsPanel.clear();
		if (pModel != null) {
			List<GoogleDocument> documents = pModel.getDocuments();
			if (documents != null && !documents.isEmpty()) {
				for (GoogleDocument document : documents) {
					docsPanel.add(getDocument(document));
				}
				return;
			}
		}
		docsPanel.add(new HTML("<b>There are no documents available.</b>"));
	}

}
